// pages/completeorder/completeorder.js
import * as api from '../../utils/api.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    details:{},
    branch_mobile: '', // 分公司电话
    business_mobile: '', // 提货商家电话
    fileList: [],
    order_sn:''
  },

  afterRead(event) {
    const that = this
    const { file } = event.detail;
    const order_sn = that.data.order_sn
    // console.log(file)
    // return
    api.upload(file.url)
      .then(res => {
        let fileList = this.data.fileList
        const url = res.data.url
        fileList.push({ ...file, url })
        that.setData({
          fileList
        })
        api.workerInstallOrderUploadImages(order_sn,url)
        .then(res => {
          console.log(res)
          that.onLoad()
        })
        .catch(err => console.log(err))
      }).catch(err => {
        console.log(err)
      })
  },
  delFile(event) {
    let app = this
    let order_sn = app.data.order_sn
    let index = event.detail.index
    let fileList = this.data.fileList
    fileList.splice(index, 1)
    this.setData({fileList})
    api.workerInstallOrderDelImages(order_sn,index)
        .then(res => {
          app.onLoad()
        })
        .catch(err => console.log(err))
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onDetails(order_sn){
    api.workerInstallOrderDetail(order_sn).then(res=>{
      this.setData({
        details:res.data,
        branch_mobile: res.data.branch_mobile,
        business_mobile: res.data.business_mobile,
        fileList:res.data.install_order.images
      })
    })
  },
  callPhone(e) {
    const mobile = e.currentTarget.dataset.mobile
    if (mobile) {
      wx.makePhoneCall({
        phoneNumber: mobile
      })
    } else {
      wx.showToast({
        title: '未设置电话',
        icon: 'error'
      })
    }
  },
  onLoad(options) {
    this.onDetails(options.order_sn)
    this.setData({
      order_sn:options.order_sn
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    let pages = getCurrentPages();   //获取小程序页面栈
    let beforePage = pages[pages.length -2];  //获取上个页面的实例对象
    beforePage.setData({      //直接修改上个页面的数据（可通过这种方式直接传递参数）
      list: []
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})